package com.jyskkebank.android.toggl;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jyskebank.android.toggl.domain.ExpandableListAdapter;
import com.jyskebank.android.toggl.domain.TimeEntry;

public class TaskListActivity extends Activity implements View.OnClickListener {
	Context mContext;
	private ExpandableListAdapter adapter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		System.out.println(" >>>>>>>>>>> TASK LIST >>>>>>>>>>> ");
		super.onCreate(savedInstanceState);
		setTaskListContent();
	}
	
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.tasklistmenu, menu);
		return true;
	}

	private void setTaskListContent() {
		setContentView(R.layout.tasklist);
		Button newTaskBtn = (Button) findViewById(R.id.new_task_button);
		newTaskBtn.setOnClickListener(this);

	/*	TextView logoutBtn = (TextView) findViewById(R.id.logout);
		logoutBtn.setOnClickListener(this);*/

		String apiToken = getIntent().getExtras().getString("apiToken");
		System.out.println("Api Token >>>> " + apiToken);
		populateTimeEntries(apiToken);
	}

	private void goToUpdateTask(TimeEntry timeEntry) {
		System.out.println("-------------- goToUpdateTask -----");
		// onDestroy(R.id.taskListLayoutId);
		
		Intent intent = new Intent(this, TaskActivity.class);
		intent.putExtra("apiToken",  getIntent().getExtras().getString("apiToken"));
		intent.putExtra("timeEntry", (Serializable)timeEntry);
		String workspaceId = getIntent().getExtras().getString("workspaceId");
		System.out.println("workspaceId in TaskListActivity:" + workspaceId);
		intent.putExtra("workspaceId", workspaceId);
		intent.putExtra("id", timeEntry.getId());
		setContentView(R.layout.task);
		startActivity(intent);
	}

	private void onDestroy(int id) {
		super.onDestroy();
		unbindDrawables(findViewById(id));
		RelativeLayout layout = (RelativeLayout) findViewById(id);
		layout.setBackgroundDrawable(null);
		System.gc();
	}

	private void unbindDrawables(View view) {
		if (view instanceof ViewGroup) {
			for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
				System.out.println(" view >>>>>>>> :: " + view);
				unbindDrawables(((ViewGroup) view).getChildAt(i));
			}
			((ViewGroup) view).removeAllViews();
		}
	}
	
	private void populateTimeEntries(String apiToken) {
		ExpandableListView listView = (ExpandableListView) findViewById(R.id.listView);		
		listView.setOnChildClickListener(new OnChildClickListener() {
			public boolean onChildClick(ExpandableListView parent, View v,
					int groupPosition, int childPosition, long id) {
				TimeEntry timeEntry = (TimeEntry)adapter.getChild(groupPosition, childPosition);
				System.out.println("Selected timeEntry in tasklist :: >> "+timeEntry);
				goToUpdateTask(timeEntry);
				return false;
			}
		});
		listView.setOnGroupClickListener(new OnGroupClickListener() {
			public boolean onGroupClick(ExpandableListView arg0, View arg1,
					int arg2, long arg3) {
				System.out.println("--------  in group clicked ----");

				return false;
			}
		});
		fetchTaskList(apiToken, listView);
	}

	private void fetchTaskList(String apiToken, ExpandableListView listView) {
		TogglService togglService = new TogglService();
		List<TimeEntry> timeEntriesList = togglService.getTimeEntries(apiToken);
		adapter = new ExpandableListAdapter(this, new ArrayList<String>(),
				new ArrayList<ArrayList<TimeEntry>>());
		// Set this blank adapter to the list view
		listView.setAdapter(adapter);
		for (TimeEntry timeEntry : timeEntriesList) {
			adapter.addItem(timeEntry);
		}
	}

	public void onClick(View v) {
		switch (v.getId()) {
/*		case R.id.logout: // doStuff
			showLogoutPopup(this);
			break;*/
		case R.id.new_task_button: // doStuff
			Intent intent = new Intent(this, TaskActivity.class);
			String apiToken = getIntent().getExtras().getString("apiToken");
			intent.putExtra("apiToken", apiToken);
			String workspaceId = getIntent().getExtras().getString("workspaceId");
			System.out.println("workspaceId in TaskListActivity:" + workspaceId);
			intent.putExtra("workspaceId", workspaceId);
			this.setContentView(R.layout.task);
			startActivity(intent);
			break;
		}
	}

	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.logout:
			showLogoutPopup(this);
			break;
		}
		return false;
	}
	
	private void showLogoutPopup(Context mContext) {
		AlertDialog.Builder logoutPopup = new AlertDialog.Builder(mContext);
		logoutPopup.setInverseBackgroundForced(false);
		logoutPopup.setTitle("Logout").setMessage("Do you really want to logout ?").setPositiveButton("OK", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int id) {
						contentView();
					}
				}).setNegativeButton("Cancel", null).show();
		Intent intent = new Intent(this , LoginActivity.class);
		startActivity(intent);
	}

	private void contentView() {
		Intent intent = new Intent(this , TaskListActivity.class);
		startActivity(intent);
	}

}
